package pl.edu.agh.agents.path;

import java.util.ArrayList;
import java.util.List;

import pl.edu.agh.agents.state.State;


public class Path extends ArrayList<State> {
	private static final long serialVersionUID = -1447295943503367117L;

	public Path(){
		super();
	}
	
	public Path(List<State> states){
		super();
		this.addAll(states);
	}
	
	/**
	 *  For each state s in path check if next state in path is neighbor of s
	 */
	public boolean validate() {
		boolean ret = simpleValidation();
		if(ret && !get(1).isEmpty() && !get(1).getAgent().equals(get(0).getAgent())){
			ret = false;
		}

		return ret;
	}
	
	public boolean simpleValidation() {
		if (size() == 1) {
			return false;
		}
		for (int i = 0; i < size() - 1; i++) {
			State state = get(i);
			State nextState = get(i + 1);
			if (!state.getNeighbors().contains(nextState) && !state.equals(nextState)) {
				return false;
			}
		}
		return true;
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < size() ; i++) {
			State state = get(i);
				sb.append(state.getID() + " ");
			}
		return sb.toString();
	}
	
}
